///<reference path="../../node_modules/rxjs/Observable.d.ts"/>
import {Component, ViewChild} from '@angular/core';
import {AlertController, IonicApp, MenuController, Nav, Platform} from 'ionic-angular';
import {StatusBar} from '@ionic-native/status-bar';
import {SplashScreen} from '@ionic-native/splash-screen';



/*customer页面*/
import {HomePage} from '../customer/pages/home/home';
import {MenuPage} from '../customer/pages/menu/menu';
import {IndentPage} from '../customer/pages/indent/indent';
import {ShoppingCartPage} from "../customer/pages/shopping-cart/shopping-cart";
import {MyAccountPage} from "../customer/pages/my-account/my-account";
import {ChangePasswordPage} from "../customer/pages/change-password/change-password";
/*employee页面*/
import {EmployeeHomePage} from '../employee/pages/home/home';
import {EmployeeMenuPage} from '../employee/pages/menu/menu';
import {EmployeeIndentPage} from '../employee/pages/indent/indent';
import {EmployeeShoppingCartPage} from "../employee/pages/shopping-cart/shopping-cart";
import {EmployeeMyAccountPage} from "../employee/pages/my-account/my-account";
import {EmployeeChangePasswordPage} from "../employee/pages/change-password/change-password";
/*提供者*/
import {CustomerProvider} from "../providers/customerProvider";
import {CustomerLoginPage} from "../customer/pages/customer-login/customer-login";
import {EmployeeProvider} from "../providers/employeeProvider";

import {EmployeeLoginPage} from "../employee/pages/employee-login/employee-login";


@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  // @ViewChild(Nav) nav: Nav;
  @ViewChild('content') nav: Nav;

  rootPage: any;
  public auth: any;
  backButtonPressed: boolean = false;
  customerPages: Array<{ title: string, component: any, method?: any }>;
  employeePages: Array<{ title: string, component: any, method?: any }>;

  constructor(public platform: Platform,
              public statusBar: StatusBar,
              public splashScreen: SplashScreen,
              public menuCtl: MenuController,
              public customer: CustomerProvider,
              public employee: EmployeeProvider,
              public alert: AlertController,
              public appCtrl:IonicApp) {
    this.initializeApp();
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();
      /*       this.init();
                this.sqlHelp.createTable();*/
      this.registerBackButtonAction();

    });


    // used for an example of ngFor and navigation
    /* My Account
     Change Password
     Personalization
     My Subcuptions
     Display Mode
     Payment Methods
     Contact Customer Support
     About Company*/
    this.customerPages = [
      {title: 'My Account', component: MyAccountPage,},
      {title: 'Change Password', component: ChangePasswordPage},
      {title: 'Restaurant Menu', component: HomePage, method: 'root'},
      {title: 'My Cart', component: ShoppingCartPage,},
      {title: 'Menu', component: MenuPage,},
      {title: 'My Order', component: IndentPage,},
    ];
    this.employeePages = [
      {title: 'My Account', component: EmployeeMyAccountPage,},
      {title: 'Change Password', component: EmployeeChangePasswordPage},
      {title: 'Restaurant Menu', component: EmployeeHomePage, method: 'root'},
      {title: 'My Cart', component: EmployeeShoppingCartPage,},
      {title: 'Menu', component: EmployeeMenuPage,},
      {title: 'My Order', component: EmployeeIndentPage,},
    ]
  }

  initializeApp() {
    this.platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native tifhings you might need.
      this.statusBar.styleDefault();
      this.splashScreen.hide();
      console.log(this.employee);
      if (this.employee.check())
        this.rootPage = EmployeeHomePage;
      else {
        if (this.customer.check())
          this.rootPage = HomePage;
        else
          this.rootPage = CustomerLoginPage;
      }
    });
  }

  openPage(page) {
    // Reset the content nav to have just this page
    // we wouldn't want the back button to show in this scenario
    console.log('open page:' + page.title);
    if (page.method == 'root')
      this.nav.setRoot(page.component);
    else
      this.nav.push(page.component);


  }

  logout() {
    if (this.employee.check()) {
      this.employee.logout();

    }
    else {
      this.customer.logout();
    }
    this.menuCtl.enable(false);
    this.nav.setRoot(CustomerLoginPage);
  }

  registerBackButtonAction() {
    this.platform.registerBackButtonAction(() => {

      let activeVC = this.nav.getActive();

      let page = activeVC.instance;

      if (page instanceof CustomerLoginPage || page instanceof HomePage || page instanceof EmployeeHomePage || page instanceof EmployeeLoginPage) {
        let alert = this.alert.create({
          subTitle: 'Confirm exit?',
          buttons: [
            {
              text: 'Not Exit',
              handler: () => {
              }
            },
            {
              text: 'Exit',
              handler: () => {
                this.platform.exitApp();
              }
            }
          ]
        });
        alert.present();
        return;
      }

      else {
        let overlay = this.appCtrl._overlayPortal.getActive() || this.appCtrl._modalPortal.getActive() ;
        if(overlay) {
          overlay.dismiss();
          this.nav.pop();

        }
        this.nav.pop();
      }


    });
  }


}
